package scau.erps.view.system.teacher;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import scau.erps.app.Erps;
import scau.erps.entity.SystemOrder;
import scau.erps.service.SystemOrderService;
import scau.erps.service.impl.SystemOrderServiceImpl;

/**
 * 系统订单面板——教师
 * @author DCJ
 *
 */
public class SystemOrderPanel extends SystemOrderPanelTemplate {
	private static final long serialVersionUID = 1L;
	public SystemOrderPanel(){
		super();
		this.biddingBtn.setVisible(false);
		this.endBiddingBtn.setVisible(false);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==filterBtn){
			final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dataModel);
			List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
			String year = null;
			if(yearCombo.getSelectedIndex() != 0 ){
				year = (String) yearCombo.getSelectedItem();
			}
			String product = null;
			if(productCombo.getSelectedIndex() != 0 ){
				product = (String) productCombo.getSelectedItem();
			}
			String market = null;
			if(marketCombo.getSelectedIndex() != 0 ){
				market = (String) marketCombo.getSelectedItem();
			}
			if(year !=null){
				filters.add(RowFilter.regexFilter(year));
			}
			if(product !=null){
				filters.add(RowFilter.regexFilter(product));
			}
			if(market !=null){
				filters.add(RowFilter.regexFilter(market));
			}
			sorter.setRowFilter(RowFilter.andFilter(filters));
			this.table.setRowSorter(sorter); 
            this.table.setModel(dataModel);			
		}
		else if(e.getSource()==refreshBtn){
			this.table.setModel(getOrderListModel());
		}
		else if(e.getSource() == createBtn){
			String year = null;
			if(yearCombo.getSelectedIndex() != 0 ){
				year = (String) yearCombo.getSelectedItem();
			}
			String product = null;
			if(productCombo.getSelectedIndex() != 0 ){
				product = (String) productCombo.getSelectedItem();
			}
			String market = null;
			if(marketCombo.getSelectedIndex() != 0 ){
				market = (String) marketCombo.getSelectedItem();
			}
			if(year==null || product==null || market==null){
				JOptionPane.showMessageDialog(null, "请指定年份、产品、市场三个参数");
			}else{
				new SystemOrderGeneratorDialog(year,product,market);
			}
		}
		else if(e.getSource()==deleteBtn){
			String year = null;
			if(yearCombo.getSelectedIndex() != 0 ){
				year = (String) yearCombo.getSelectedItem();
			}
			String product = null;
			if(productCombo.getSelectedIndex() != 0 ){
				product = (String) productCombo.getSelectedItem();
			}
			String market = null;
			if(marketCombo.getSelectedIndex() != 0 ){
				market = (String) marketCombo.getSelectedItem();
			}
			if(year==null && product==null && market==null){
				JOptionPane.showMessageDialog(null, "年份、产品、市场至少选择一个");
			}else{
				SystemOrderService service = Erps.getIocInstance().get(SystemOrderServiceImpl.class);
				List<SystemOrder> list = service.getSystemOrders(year, product, market);
				int a = list.size();		
				String message = "找到"+year+"年"+market+product+"订单"+a+"张,确认删除？";
				int n = JOptionPane.showConfirmDialog(null, message, "提醒",JOptionPane.OK_CANCEL_OPTION);
				if(n == JOptionPane.OK_OPTION){
					service.deleteOrders(list);
					JOptionPane.showMessageDialog(null, "成功删除订单"+a+"张!");
				}
			}
			this.table.setModel(getOrderListModel());
		}
	}
}
